package com.sc.nft.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.sc.nft.dao.UserEcologyReforgeUpperLimitDao;
import com.sc.nft.entity.UserEcologyCastUpperLimit;
import com.sc.nft.entity.UserEcologyCastUpperLimitDetails;
import com.sc.nft.entity.UserEcologyReforgeUpperLimit;
import com.sc.nft.entity.UserEcologyReforgeUpperLimitDetails;
import com.sc.nft.enums.UserEcologyCastUpperLimitDetailsTypeEnum;
import com.sc.nft.enums.UserEcologyReforgeUpperLimitDetailsTypeEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.service.UserEcologyReforgeUpperLimitService;
import org.springframework.stereotype.Service;

/**
 * 生态-用户重铸上限表服务接口实现
 *
 * @author Sun
 * @since 2024-06-07 09:04:10
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class UserEcologyReforgeUpperLimitServiceImpl implements UserEcologyReforgeUpperLimitService {

    private final UserEcologyReforgeUpperLimitDao userEcologyReforgeUpperLimitDao;

    @Override
    public void addCountByUserIdAndCollectionId(Long userId, Long collectionId, Integer count, UserEcologyReforgeUpperLimitDetailsTypeEnum type, String eventsName, Long userRecordsId) {


        UserEcologyReforgeUpperLimit userEcologyReforgeUpperLimit = userEcologyReforgeUpperLimitDao.getOneByUserIdAndCollectionId(userId, collectionId);
        Integer beforeCount = 0;
        if(ObjectUtil.isNull(userEcologyReforgeUpperLimit)){
            UserEcologyReforgeUpperLimit insertUserEcologyReforgeUpperLimit = new UserEcologyReforgeUpperLimit();
            insertUserEcologyReforgeUpperLimit.setUserId(userId);
            insertUserEcologyReforgeUpperLimit.setCollectionId(collectionId);
            insertUserEcologyReforgeUpperLimit.setUsableCount(count);
            insertUserEcologyReforgeUpperLimit.insert();
        }else{
            beforeCount = userEcologyReforgeUpperLimit.getUsableCount();
            Assert.isTrue(
                    userEcologyReforgeUpperLimitDao.addCountByUserIdAndCollectionId(userId, collectionId, count),
                    () -> new GlobalRunTimeException("用户重铸上限更新失败"));
        }


        UserEcologyReforgeUpperLimitDetails userEcologyReforgeUpperLimitDetails = new UserEcologyReforgeUpperLimitDetails();
        userEcologyReforgeUpperLimitDetails.setUserId(userId);
        userEcologyReforgeUpperLimitDetails.setCollectionId(collectionId);
        userEcologyReforgeUpperLimitDetails.setUserRecordsId(userRecordsId);
        userEcologyReforgeUpperLimitDetails.setType(type);
        userEcologyReforgeUpperLimitDetails.setTypeName(StrUtil.format("{}:{}",type.getDescription(),eventsName));
        userEcologyReforgeUpperLimitDetails.setUseCount(count);
        userEcologyReforgeUpperLimitDetails.setBeforeCount(beforeCount);
        userEcologyReforgeUpperLimitDetails.setAfterCount(beforeCount + count);
        userEcologyReforgeUpperLimitDetails.setIsAdd(type.getIsAdd());
        userEcologyReforgeUpperLimitDetails.insert();
    }

    @Override
    public void subCountByUserIdAndCollectionId(Long userId, Long collectionId, Integer count, UserEcologyReforgeUpperLimitDetailsTypeEnum type, String eventsName, Long userRecordsId) {
        UserEcologyReforgeUpperLimit userEcologyReforgeUpperLimit = userEcologyReforgeUpperLimitDao.getOneByUserIdAndCollectionId(userId, collectionId);
        Assert.notNull(userEcologyReforgeUpperLimit, "用户重铸上限不存在");

        Assert.isTrue(
                userEcologyReforgeUpperLimitDao.subCountByUserIdAndCollectionId(userId, collectionId, count),
                () -> new GlobalRunTimeException("用户重铸上限更新失败"));

        UserEcologyReforgeUpperLimitDetails userEcologyReforgeUpperLimitDetails = new UserEcologyReforgeUpperLimitDetails();
        userEcologyReforgeUpperLimitDetails.setUserId(userId);
        userEcologyReforgeUpperLimitDetails.setCollectionId(collectionId);
        userEcologyReforgeUpperLimitDetails.setUserRecordsId(userRecordsId);
        userEcologyReforgeUpperLimitDetails.setType(type);
        userEcologyReforgeUpperLimitDetails.setTypeName(type.getDescription());
        userEcologyReforgeUpperLimitDetails.setUseCount(count);
        userEcologyReforgeUpperLimitDetails.setBeforeCount(userEcologyReforgeUpperLimit.getUsableCount());
        userEcologyReforgeUpperLimitDetails.setAfterCount(userEcologyReforgeUpperLimit.getUsableCount() - count);
        userEcologyReforgeUpperLimitDetails.setIsAdd(type.getIsAdd());
        userEcologyReforgeUpperLimitDetails.insert();
    }
}